<?

require_once('./application/classes/item/lessen.class.php');

class lessenModel extends db implements db_interface{

private static $instance;

public function __construct($iDb){
	
		parent::__construct($iDb);
	
	}
	
public static function getInstance($iDb){
	
		if(self::$instance == NULL){
			self::$instance = new self($iDb);
		}
		
		return self::$instance;
	}
	
			public function getAantal(){
	
	$query = $this -> db -> query('SELECT COUNT(*) as aantal FROM item WHERE item_deleted = 1 AND item_type = "lessen"');
	
	$row = $query -> fetch_assoc();
	return $row['aantal'];
	
	}

	
	public function getAllLessen(){
	
		$query = $this -> db -> query('SELECT i.item_id,i.item_title,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date,l.les_url,g.geb_id,g.geb_naam  FROM item i join lessen l on(i.item_id = l.les_id) join gebruikers g on (i.item_geb_id = g.geb_id) where i.item_deleted = 1 ORDER BY i.item_id DESC;');
		
		
		$lessen = array();
		while ($row = $query->fetch_assoc())
		{
			
			$les = new item_lessen($this -> db);
			$les -> id = $row['item_id'];
			$les -> title = $row['item_title'];
			$les -> geb_naam = $row['geb_naam'];
			$les -> les_path = $row['les_url'];
			$les -> geb_id = $row['geb_id'];
			$les -> datum = $row['date'];
		
			$lessen[]= $les;
		}
		return $lessen;
	
	}
	
	public function addLessen(item_lessen $lessen){
		$file = new uploadFile($lessen -> les_file);
		
		
		
		$random = new random(20);
		
		
		
		
		$file -> setName($random -> generateNext());
		$file -> setPath('./public/lessen/');;
		
		while(file_exists($file -> getFullPath())){
			
			$file -> setName($random -> generateNext());
		
		}
		
		$path = '/public/lessen/' . $file-> getName();
		
		
		
		$file -> upload();

		$query = $this -> db -> prepare("CALL lessenAdd(?,?,?,?)");	
		$bindingvars=array($path,$lessen -> title , $lessen -> geb_id , 'lessen');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
	}
	public function delLessen(item_lessen $lessen)
	{
		
		$query = $this -> db -> prepare('call itemDel(?);');
		$bindValue = $lessen -> id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
	}
	public function updLessen(item_lessen $lessen)
	{
		$query = $this -> db -> prepare('update item set titel=? where item_id=?;');
		$bindingVars = array($lessen -> title,$lessen -> id);
		$this -> bindVars($query, $bindingVars);
		$query -> execute();
		
	}
	public function getLessenId(item_lessen $lessen)
	{
		$query = $this -> db -> prepare('SELECT i.item_id,i.item_title,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date,l.les_url,g.geb_id,g.geb_naam  FROM item i join lessen l on(i.item_id = l.les_id) join gebruikers g on (i.item_geb_id = g.geb_id) WHERE i.item_id = ? and i.item_deleted = 1');
		
		$bindValue = $lessen -> id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$title,$date,$les_url,$geb_id,$naam); 
				
		$query -> fetch();
		
			$lessen = new item_bericht($this -> db);
			$lessen -> id = $id;
			$lessen -> title = $title;
			$lessen -> geb_naam = $naam;
			$lessen -> datum = $date;
			$lessen -> les_path = $les_url;
			$lessen -> geb_id = $geb_id;
			return $lessen;
			
		
		
		
		
	}

}



?>


